#!python
import boto
import time
import os, sys
from boto.contrib.ymlmessage import YAMLMessage
from Growl import GrowlNotifier

sqs = boto.connect_sqs()
q = sqs.get_queue(boto.config.get("sqs-growl", "queue"))
q.set_message_class(YAMLMessage)
icon = open(os.path.dirname(os.getcwd() + "/" + sys.argv[0]) + "/icon.png", "r")


g = GrowlNotifier(applicationName = "SQS Growl",
						notifications = ['alert', 'warning', 'critical'],
						applicationIcon = icon.read()
						)

g.register()

g.notify("alert", "Started SQS Monitor", "Growl SQS Notification Started")


while True:
	m = q.read(10)
	while m != None:
		try:
			type = m.data['type']
			title = m.data['title']
			description = m.data['description']
			g.notify(type, title, description)
			q.delete_message(m)
			m = q.read(60)
		except:
			print "Error reading message: %s" % m.get_body()
			print "Ignoring"
			#q.delete_message(m)
			m = q.read(60)
	
	time.sleep(60)
